{% if wallet is defined %}

<div id="hwi_display_address" class="flex-center flex-column hidden p-4 bg-dark-800" data-style="overflow-wrap: break-word;">
    <h2>{{ _("Confirm Address") }}</h2><br>
    <div>
        {{ _("Please confirm address matches on your ")}}<span id="hwi_device_name">{{ _('device')}}</span><br><br>
        {{ _("Expected address") }}:<br><span id="expected_address"></span>
    </div>
    <div class="flex justify-center mt-4 ">
        <img src="{{ url_for('static', filename='img/loader_boxes.svg') }}" class="w-16 h-16"/>
    </div>
</div>

<script type="text/javascript">
    async function getConnectedWalletDevices(deviceTypes=null){
        if(deviceTypes==null){
            deviceTypes = [
            {% for device in wallet.devices %}
                `{{ device.device_type }}`,
            {% endfor %}
            ];
        }
        let devices = await enumerate(deviceTypes);
        return devices;
    }
    async function signTx(combine, deviceType, psbt, fingerprint=null){
        let devices = await getConnectedWalletDevices([deviceType]);
        if(devices == null || devices.length == 0){
            return;
        }
        let device = await selectDevice(devices);
        console.log(device)
        if(device == null){
            return;
        }

        if (fingerprint && device.fingerprint != fingerprint) {
            handleHWIError(`{{ _("Selected device does not have matching signing key.") }}`);
            return;
        }
        showHWIProgress(`{{ _("Signing transaction...") }}`, `{{ _("Confirm on your") }} ${capitalize(device.type)}.`);
        let signedTx = null;
        try {
            signedTx = await hwi.signTx(device, psbt);
        } catch (error) {
            handleHWIError(error);
            return;
        }
        hidePageOverlay();
        await combine(signedTx);
        console.log(signedTx);
        console.log(device);
        console.log(deviceType);
        console.log(psbt);
    }

    async function displayAddressOnDevice(address, descriptor, xpubs_descriptor){
        let supportedDevices = [
            {% for device in wallet.devices %}
                {% if device.supports_hwi_multisig_display_address or not wallet.is_multisig %}
                    `{{ device.device_type }}`,
                {% endif %}
            {% endfor %}
        ];
        let devices = await getConnectedWalletDevices(supportedDevices);
        if(devices == null || devices.length==0){
            return;
        }
        // also unlocks it
        let device = await selectDevice(devices);
        if(device == null){
            return;
        }
        console.log(device);
        document.getElementById('expected_address').innerText = address;
        showPageOverlay('hwi_display_address');        
        let result = null;
        try {
            result = await hwi.displayAddress(device, descriptor, xpubs_descriptor);
        } catch (error) {
            handleHWIError(error);
            return null;
        }
        console.log(result);
        hidePageOverlay();
        if (address != result &&
            (("{{ specter.chain }}" != "regtest" || !result.startsWith("tb")) ||
            address.slice(0, -6) != "bcrt" + result.slice(2, -6))
        ) {
            showError(`{{ _("Addresses did not match! Be careful! Please contact support and beware of using the displayed address!") }}`);
        } else {
            if (device.type == 'coldcard') {
                showNotification(`{{ _("Address displayed successfully! Please check the device screen.") }}`, 7000);
            } else {
                showNotification(`{{ _("Address was verified successfully!") }}`, 5000);
            }
        }
    }

    async function registerMultisig(descriptor, fingerprint) {
        const devices = await enumerate()
        
        if (!devices || devices.length === 0) {
            return
        }
        
        const device = await selectDevice(devices)
        
        if (!device) {
            return
        }

        if (fingerprint && device.fingerprint != fingerprint) {
            handleHWIError("Device fingerprints don't match. You have probably selected the wrong device.")
            return
        }

        showHWIProgress("Registering multisig ...", `Confirm on your ${capitalize(device.type)}`)

        try {
            await hwi.registerMultisig(device, descriptor)
        } catch (error) {
            handleHWIError(error)
            return
        }
        hidePageOverlay()
        showNotification("Multisig registered successfully!", 3000);
    }
</script>

{% endif %}
